function [outdat] = rt_queries_Sandbox(type, parameters)
    switch type
        case 'joint'
            [siteID, t_interval] = parameters{:};
            outdat = joint_query(siteID, t_interval);
        case 'site'
            outdat = site_query(parameters{:});
        case 'equipment'
            outdat = equipment_query(parameters{:});
        case 'inventory'
            outdat = inventory_query(parameters{:});
        case 'calib'
            [equipID, calibType] = parameters{:};
            outdat = calib_query(equipID, calibType);
        case 'caltyp'
            outdat = caltyp_query(parameters{:});
        case 'calform'
            outdat = calform_query(parameters{:});
    end
end

function [outdat] = joint_query(siteID, t_interval)
    try
        sqlQuery = {['SELECT RawValue.RawValueID, '...
                            'RawValue.SiteID, '...
                            'RawValue.PostedUTCDatetime, '...
                            'RawValue.PressureQuantity, '...
                            'RawValue.TemperatureQuantity, '...
                            'RawValue.ConductivityQuantity, '...
                            'RawValue.SalinityQuantity, '...
                            'RawValue.Aux0Quantity, '...
                            'CalculatedValue.DensityCalculated, '...
                            'CalculatedValue.DOSaturationCalculated'],...
                     'FROM RawValue INNER JOIN CalculatedValue ON RawValue.RawValueID = CalculatedValue.RawValueID',...
                    ['WHERE (((RawValue.SiteID)=''' siteID ''') AND (RawValue.ProcessedFlag=0) AND '...
                     '((RawValue.PostedUTCDatetime)>=' datestr(t_interval(1),'#mm/dd/yyyy HH:MM:SS#') ''...
                     ' And (RawValue.PostedUTCDatetime)<=' datestr(t_interval(2),'#mm/dd/yyyy HH:MM:SS#') ')) '],...
                     'ORDER BY RawValue.PostedUTCDatetime;'};
        conn   = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying SiteID ' siteID ', ProcessedFlag=0, and '...
              datestr(t_interval(1),'mm/dd/yyyy HH:MM:SS') ' <= PostedUTCDatetime <= '...
              datestr(t_interval(2),'mm/dd/yyyy HH:MM:SS') ' in RawValue Table inner joined with '...
              'CalculatedValue Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3} ' ' sqlQuery{4}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        outdat = curs.Data;
    catch ME
        h = errordlg('Error extracting joint Raw/Calculated Value data...','Error');
        outdat = {};
    end
end

function [outdat] = site_query(siteID)
    try
        sqlQuery = {strcat('SELECT SiteID, SiteName, SiteDescription, SiteTypeID, ',...
                    'LatitudeDecimal, LongitudeDecimal, SiteMLLWDecimal '),...
                    'FROM Site ',...
                    strcat('WHERE SiteID=''', siteID, '''')};

        conn   = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying SiteID ' siteID ' in Site Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        outdat = curs.Data;
    %    h = msgbox('Site data extracted...','Success');
    catch ME
        h = errordlg('Error extracting Site data...','Error');
        outdat = {};
    end
end

function [outdat] = equipment_query(siteID, t_interval)
    try
        sqlQuery = {strcat('SELECT EquipmentID, StatusID, EquipmentName, UTCDeployDatetime, AuxillaryChannelID, ',...
                    'CalibrationTypeID, ResultColumnID, SiteID'),...
                    'FROM SiteEquipment ',...
                    ['WHERE (((SiteID)=''' siteID ''') AND '...
                     '((UTCDeployDatetime)<=' datestr(t_interval(2),'#mm/dd/yyyy HH:MM:SS#') ')) '],...
                    'ORDER BY UTCDeployDatetime;'};

        conn = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying SiteID ' siteID ' in SiteEquipment Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        outdat = curs.Data;
    %    h = msgbox('RawValue data extracted...','Success');
    catch EE
        h = errordlg('Error extracting SiteEquipment data...','Error');
        outdat = {};
    end
end

function [outdat] = inventory_query(equipID)
    try
        where = []; cnt = 1; Where = 'WHERE ';
        for i=1:length(equipID)
            if ~ismember(equipID{i}, where)
                Where = [Where, 'EquipmentID=', num2str(equipID{i}), ' OR '];
                where(cnt) = equipID{i};
                cnt = cnt + 1;
            end
        end
        Where = Where(1:end-4);
        sqlQuery = {strcat('SELECT EquipmentName, SerialNumberDescription, ',...
                    'EquipmentTypeID, ManufacturerName, ModelDescription'),...
                    'FROM EquipmentInventory ',...
                    Where};

        conn = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying EquipmentID ' num2str([equipID{:}]) ...
              ' in EquipmentInventory Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        outdat = curs.Data;
    %    h = msgbox('RawValue data extracted...','Success');
    catch EI
        h = errordlg('Error extracting EquipmentInventory data...','Error');
        outdat = {};
    end
end

function [outdat] = calib_query(equipID, calibType)
    try
        where_equipID = []; cnt = 1; Where_equipID = 'WHERE ((';
        for i=1:length(equipID)
            if ~ismember(equipID{i}, where_equipID)
                Where_equipID = [Where_equipID, 'EquipmentID=', num2str(equipID{i}), ' OR '];
                where_equipID(cnt) = equipID{i};
                cnt = cnt + 1;
            end
        end
        Where_equipID = [Where_equipID(1:end-4) ')'];
        where_calibType = {}; cnt = 1; Where_calibType = ' AND (';
        for i=1:length(calibType)
            if ~ismember(calibType(i), where_calibType)
                Where_calibType = [Where_calibType, 'CalibrationTypeID=''', calibType{i}, ''' OR '];
                where_calibType(cnt) = calibType(i);
                cnt = cnt + 1;
            end
        end
        Where_calibType = [Where_calibType(1:end-4) '))'];
        Where = [Where_equipID Where_calibType];
        
        sqlQuery = {['SELECT EquipmentID, UTCCalibrationBeginDatetime, UTCCalibrationEndDatetime, ',...
                    'EquipmentName, CalibrationTypeID, CalibrationFormulaID, CalibrationID, ',...
                    'C1Quantity, C2Quantity, C3Quantity, C4Quantity, C5Quantity, C9Quantity, ',...
                    'C6Quantity, C7Quantity, C8Quantity, C10Quantity, C11Quantity, C12Quantity, ',...
                    'C13Quantity'],...
                    'FROM Calibration ',...
                    Where, 'ORDER BY EquipmentID, UTCCalibrationBeginDatetime'};

        conn = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying EquipmentID ' num2str([equipID{:}])...
              ' and CalibrationTypeID ' calibType{:} ' in Calibration Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        if strcmp(curs.Data{:}, 'No Data')
            outdat = cell(1, 19); outdat{1} = equipID;
        else
            outdat = curs.Data;
        end
    %    h = msgbox('RawValue data extracted...','Success');
    catch Ca
        h = errordlg('Error extracting Calibration data...','Error');
        outdat = {};
    end
end

function [outdat] = caltyp_query(calibType)
    try
        where_calibType = {}; cnt = 1; Where_calibType = 'WHERE ';
        for i=1:length(calibType)
            if ~ismember(calibType(i), where_calibType)
                Where_calibType = [Where_calibType, 'CalibrationTypeID=''', calibType{i}, ''' OR '];
                where_calibType(cnt) = calibType(i);
                cnt = cnt + 1;
            end
        end
        Where = Where_calibType(1:end-4);

        sqlQuery = {'SELECT CalibrationTypeID, CalibrationTypeDescription',...
                    'FROM CalibrationType ',...
                    Where};

        conn = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying CalibrationTypeID ' calibType{:} ' in CalibrationType Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        if strcmp(curs.Data{:}, 'No Data')
            outdat = cell(1, 19); outdat{1} = calibType;
        else
            outdat = curs.Data;
        end
    %    h = msgbox('RawValue data extracted...','Success');
    catch CT
        h = errordlg('Error extracting CalibrationType data...','Error');
        outdat = {};
    end
end

function [outdat] = calform_query(calibForm)
    try
        where_calibForm = {}; cnt = 1; Where_calibForm = 'WHERE ';
        for i=1:length(calibForm)
            if ~ismember(calibForm(i), where_calibForm)
                Where_calibForm = [Where_calibForm, 'CalibrationFormulaID=''', calibForm{i}, ''' OR '];
                where_calibForm(cnt) = calibForm(i);
                cnt = cnt + 1;
            end
        end
        Where = Where_calibForm(1:end-4);
        
        sqlQuery = {'SELECT CalibrationFormulaID, FormulaName, FormulaDescription, StoredProcedureName, UnitsDescription',...
                    'FROM CalibrationFormula ',...
                    Where};

        conn = database('Sandbox', '', '');
        dbmeta = get(dmd(conn));
        disp(['User ' dbmeta.UserName ' querying CalibrationFormulaID ' calibForm{:} ' in CalibrationFormula Table'])
        curs   = exec(conn, [sqlQuery{1} ' ' sqlQuery{2} ' ' sqlQuery{3}]);
        setdbprefs('DataReturnFormat','cellarray')
        curs = fetch(curs);
        close(curs)
        close(conn)
        if strcmp(curs.Data{:}, 'No Data')
            outdat = cell(1, 19); outdat{1} = calibForm;
        else
            outdat = curs.Data;
        end
    %    h = msgbox('RawValue data extracted...','Success');
    catch CT
        h = errordlg('Error extracting CalibrationFormula data...','Error');
        outdat = {};
    end
end